package com.topteam.graph;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:Create by 2021/6/22
 * Describe: 柱形图
 * author: leo
 * Email: le112233@126.com
 */
public class Fragment_BarChart extends Fragment{

   private View view;
   private BarChart bar_v;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.bar_fragment,container,false);
        bar_v = view.findViewById(R.id.bar);
        showBarChart();
        return view;
    }

    /**
     * 展现图表
     */
    private void showBarChart(){
        bar_v.setDrawValueAboveBar(true);  //设置所有的数值在图形的上面,而不是图形上
        bar_v.setTouchEnabled(false);  //进制触控
        bar_v.setScaleEnabled(false); //设置能否缩放
        bar_v.setPinchZoom(false);  //设置true支持两个指头向X、Y轴的缩放，如果为false，只能支持X或者Y轴的当方向缩放
        bar_v.setDrawBarShadow(false);  //设置阴影
        bar_v.setDrawGridBackground(false);  //设置背景是否网格显示
        Description description = new Description();
        description.setTextColor(Color.GREEN);
        description.setText("机台产量表");
        description.setTextSize(20);
        description.setYOffset(-50f);
        description.setXOffset(200);
        bar_v.setDescription(description); //设置描述


        //X轴的数据格式
        XAxis xAxis = bar_v.getXAxis();
        List<String> titles = new ArrayList<>();
        titles.add("周一");
        titles.add("周二");
        titles.add("周三");
        titles.add("周四");
        titles.add("周五");
        titles.add("周六");
        titles.add("周日");
        xAxis.setValueFormatter(new IndexAxisValueFormatter(titles));
        //设置位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置是否绘制网格线
        xAxis.setDrawGridLines(false);
        bar_v.getAxisLeft().setDrawGridLines(false);
        // barChart.animateY(2500);
        //设置X轴文字剧中对齐
        xAxis.setCenterAxisLabels(false);
        //X轴最小间距
        xAxis.setGranularity(1f);
        //设置字体颜色
        xAxis.setTextColor(Color.WHITE);
        xAxis.setTextSize(20);

        //Y轴的数据格式
        YAxis axisLeft = bar_v.getAxisLeft();
        axisLeft.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int in = (int) value;
                return in + "%";
            }
        });

        bar_v.animateY(2500);
        //设置Y轴刻度的最大值
        axisLeft.setAxisMinValue(0);
        axisLeft.setAxisMaxValue(100);
        bar_v.getAxisRight().setEnabled(false); //隐藏右边
        //设置字体颜色
        axisLeft.setTextColor(Color.GREEN);
        //设置字体大小
        axisLeft.setTextSize(20);
        //填充数据
        ArrayList<BarEntry> yVals1 = new ArrayList<>();
        yVals1.add(new BarEntry(0,20));
        yVals1.add(new BarEntry(1,30));
        yVals1.add(new BarEntry(2,40));
        yVals1.add(new BarEntry(3,50));
        yVals1.add(new BarEntry(4,60));
        yVals1.add(new BarEntry(5,70));
        yVals1.add(new BarEntry(6,80));
        BarDataSet set1;
        set1 = new BarDataSet(yVals1, "");
        //设置多彩 也可以单一颜色
        set1.setColor(Color.GREEN);
        set1.setDrawValues(false);
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        bar_v.setData(data);
        bar_v.setFitBars(true);
        //设置文字的大小
        set1.setValueTextSize(12f);
        //设置每条柱子的宽度
        data.setBarWidth(0.5f);
        //设置字体颜色
        data.setValueTextColor(Color.WHITE);
        bar_v.invalidate();

        for (IDataSet set : bar_v.getData().getDataSets())
            set.setDrawValues(!set.isDrawValuesEnabled());
        bar_v.invalidate();
        bar_v.setAutoScaleMinMaxEnabled(!bar_v.isAutoScaleMinMaxEnabled());
        bar_v.notifyDataSetChanged();
        bar_v.invalidate();

    }


}
